/**
 * Created by LiH on 2018/7/19.
 */
import devLog from './devLog'

// 手机号正则,18-06-21
const REGEX_TEL = /^1(3\d|4[5-7,9]|5[0-3,5-9]|66|7[01,3-8]|8\d|9[89])\d{8}$/;

/**
 * 字符截断
 * @param str {string}     目标字符串
 * @param length {number}  目标长度
 * @returns {string}
 */
function cutString(str, length) {
  return str && str.length && str.length > length ? str.substring(0, length - 1) + '…' : str
}

const userAgent = window.navigator.userAgent.toLowerCase();

/**
 * 用户设备平台判断
 * @returns {string} android是安卓，ios是苹果
 */
function deviceDetect() {
  return /iphone|mac|ios/.test(userAgent) ? 'ios' : 'android'
}

// 微信环境判断
function inWeChat() {
  return /micromessenger/.test(userAgent)
}

/**
 * 当前时间到指定时间的时间间隔格式化
 * @param date {string} 日期字符串/毫秒数
 * @returns {string}
 */
function dateIntervalFormat(date) {
  const passedTime = Date.now() - new Date(date).getTime();
  const day = Math.floor(passedTime / 86400000);
  const hour = Math.floor(passedTime / 3600000);
  const minute = Math.floor(passedTime % 3600000 / 60000);

  if (passedTime <= 60000) {
    return '刚刚'
  } else if (passedTime < 86400000) {
    return `${hour ? `${hour}小时` : ''}${minute}分钟前`
  } else {
    return `${day}天前`
  }
}

// requestAnimationFrame兼容
const requestAnimationFrame = window.requestAnimationFrame || function (fn) {
  setTimeout(fn, 17)
};

/**
 * 页面滚动动画
 * @param position @Number 指定高度
 */
function scrollTo(position) {
  let _scrollTop, distance, speed;
  const scroll = () => {
    requestAnimationFrame(() => {
      _scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      distance = position - _scrollTop;
      if (distance === 0) {
        return false
      }
      speed = Math.max(Math.abs(distance / 6), 2) * (distance > 0 ? 1 : -1);

      _scrollTop = document.documentElement.scrollTop = document.body.scrollTop = _scrollTop + speed;
      if (Math.abs(_scrollTop - position) > 2) {
        scroll()
      }
    })
  };

  scroll();
}

/**
 * 日期格式化
 * @param date {date, string, number}
 * @param fmt {string}
 * @returns {string}
 */
function dateFormat(date, fmt = 'YYYY-MM-DD HH:mm:ss') {
  if (!date) {
    return ''
  }
  if (typeof date === 'string') {
    date = new Date(date.replace(/-/g, '/'))
  }
  if (typeof date === 'number') {
    date = new Date(date)
  }
  const o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S': date.getMilliseconds()
  };
  const week = {
    '0': '\u65e5',
    '1': '\u4e00',
    '2': '\u4e8c',
    '3': '\u4e09',
    '4': '\u56db',
    '5': '\u4e94',
    '6': '\u516d'
  };
  if (/(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  if (/(E+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

export {
  REGEX_TEL,
  cutString,
  deviceDetect,
  inWeChat,
  dateIntervalFormat,
  scrollTo,
  devLog,
  dateFormat,
}
